import cv2 as cv
import pyautogui as pag
import time
#该脚本的变种可以抢微信红包
def get_position(target_pic):
    #target_pic为所找图标路径
    pag.screenshot().save('model.png')
    model_img = cv.imread('model.png')
    target_img = cv.imread(target_pic)
    width= target_img.shape[1]
    height=target_img.shape[0]
    result = cv.matchTemplate(model_img, target_img, cv.TM_SQDIFF)#结果为一个矩阵
    min_val,max_val,min_pos,max_pos=cv.minMaxLoc(result)
    center_x=min_pos[0]+width/2
    center_y=min_pos[1]+height/2
    target_point=(center_x,center_y)
    #返回待点击点的坐标点，可作为参数给pag.click()函数
    return target_point
def click_target(targetpoint):
    #该函数为后续实际调用函数
    pag.moveTo(targetpoint,duration=0.1)
    pag.click(targetpoint)
def inputmessage():
    #该函数用于输入指定信息
    message='IoT202301TangLei'
    pag.typewrite(message,0.01)
#接下来实现监听指定目标逻辑
time.sleep(5)
#先退出编辑器页面
while True:
    target = get_position('jielongkey.png')
    if target != None:
        click_target(target)
        inputmessage()
        click_target(get_position('sendkey.png'))
        pag.click()
        break
time.sleep(2)
print('已为您成功接龙！')











